package com.liming.coupon.controller;

import com.liming.common.pojo.dto.coupon.CouponValidDTO;
import com.liming.common.pojo.dto.coupon.UserHomeCouponPageQuery;
import com.liming.common.pojo.dto.order.user.mini.MiniOrderSelectCouponDTO;
import com.liming.common.pojo.entity.coupon.Coupon;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.coupon.openfeign.*;
import com.liming.common.pojo.vo.order.openfeign.mini.FeignMiniOrderSelectCouponVO;
import com.liming.common.result.LMResult;
import com.liming.coupon.service.CouponMircoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liming
 * @date 2025/1/19
 */
@Slf4j
@RestController
@AllArgsConstructor
@Tag(name = "优惠券用户接口-Service")
@RequestMapping("/service/coupon_user")
public class CouponUserMircoController {

    private final CouponMircoService couponMircoService;

    @Operation(summary = "查询用户用有的优惠券")
    @GetMapping("user_have_coupons/{userId}")
    public LMResult<List<FeignMiniCouponVO>> userHaveCoupons(@PathVariable Long userId) {
        return LMResult.success(couponMircoService.userHaveCoupons(userId));
    }

    @Operation(summary = "分页 - 用户优惠券分页")
    @GetMapping("user_home_coupon_page")
    public LMResult<PageVO<FeignMiniUserHomeCouponVO>> userHomeCouponPage(UserHomeCouponPageQuery pageQuery) {
        return LMResult.success(couponMircoService.userHomeCouponPage(pageQuery));
    }

    @Operation(summary = "列表 - 根据金额获取能使用的优惠券")
    @GetMapping("can_use_coupons_by_amount/{amount}")
    public LMResult<List<FeignMiniCanUseCouponVO>> canUseCouponsByAmount(@PathVariable Integer amount) {
        return LMResult.success(couponMircoService.canUseCouponsByAmount(amount));
    }

    @Operation(summary = "列表 - 根据金额获取不能使用的优惠券")
    @GetMapping("cannot_use_coupons_by_amount/{amount}")
    public LMResult<List<FeignMiniCannotUseCouponVO>> cannotUseCouponsByAmount(@PathVariable Integer amount) {
        return LMResult.success(couponMircoService.cannotUseCouponsByAmount(amount));
    }

    @Operation(summary = "领取优惠券")
    @PostMapping("get/{id}")
    public LMResult<Boolean> get(@PathVariable Long id) {
        Boolean result = couponMircoService.get(id);
        return LMResult.success(result);
    }

    @Operation(summary = "查询当前优惠券是否可用")
    @PostMapping("check_available")
    public LMResult<Coupon> checkAvailable(@RequestBody CouponValidDTO couponValidDTO) {
        return LMResult.success(couponMircoService.checkAvailable(couponValidDTO));
    }

    @Operation(summary = "获取所有商品能够使用的最好的优惠券 满减券、折扣券")
    @PostMapping("product_can_use_coupons")
    public LMResult<Map<Long, List<FeignMiniCouponVO>>> productCanUseCoupons(@RequestBody Map<Long, Integer> productIdAndPriceMap) {
        return LMResult.success(couponMircoService.productCanUseCoupons(productIdAndPriceMap));
    }

    @Operation(summary = "根据优惠券ID列表 查询 用户还能领取的优惠券信息")
    @PostMapping("coupon_detail_list")
    public LMResult<List<FeignMiniProductUserCouponVO>> couponDetailList(@RequestBody List<Long> couponIds) {
        return LMResult.success(couponMircoService.couponDetailList(couponIds));
    }

    @Operation(summary = "小程序 - 用户订单中选择优惠券")
    @PostMapping("select_coupon")
    public LMResult<FeignMiniOrderSelectCouponVO> miniOrderSelectCoupon(@RequestBody MiniOrderSelectCouponDTO dto) {
        return LMResult.success(couponMircoService.miniOrderSelectCoupon(dto));
    }

    @Operation(summary = "小程序 - 用户订单中选择优惠券")
    @PutMapping("user_order_use_coupon/{couponId}")
    public LMResult<Boolean> userOrderUseCoupon(@PathVariable Long couponId) {
        return LMResult.success(couponMircoService.userOrderUseCoupon(couponId));
    }

    @Operation(summary = "优惠券退还")
    @PutMapping("payment_failure/{userId}/{couponId}")
    public LMResult<Boolean> paymentFailure(@PathVariable Long userId, @PathVariable Long couponId) {
        return LMResult.success(couponMircoService.returnCoupon(userId, couponId));
    }
}
